home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Cheat II / cheatpreffile.c < prev    next >
Text File  |  1994-10-25  |  2KB  |  86 lines

  1. // cheatpreffile.c
  2. // deals with making a preferences file in the System folder
  3.  
  4. #include <Files.h>
  5. #include "cheatpreffile.h"
  6.  
  7. #define preffilename "\pCheat Prefs"
  8. #define preffilecreator 'chit'
  9. #define preffiletype 'pref'
  10.  
  11. static OSErr DoCreatePrefsFile(short VRefNum, long DirID)
  12. {
  13.    return HCreate(VRefNum, DirID, preffilename, preffilecreator, preffiletype);
  14. }
  15.  
  16.  
  17. static int IsFindFolder(void)
  18. {
  19.   OSErr Result;
  20.   long Feature;
  21.  
  22.   Result = Gestalt(gestaltFindFolderAttr, &Feature);
  23.   if (Result == noErr)
  24.     return ((Feature & (1 << gestaltFindFolderPresent)) != 0);
  25.  //     return (btst(Feature, gestaltFindFolderPresent));
  26.   return Result;
  27. }
  28.  
  29.  
  30. int DoReadPrefs(Ptr p, long length)
  31. {
  32.   short VRefNum;
  33.   long DirID, ThePrefs;
  34.   short fref, ret;
  35.   OSErr result;
  36.  
  37.  if (IsFindFolder())
  38.     result = FindFolder(kOnSystemDisk, kPreferencesFolderType,
  39.             kDontCreateFolder, &VRefNum, &DirID);
  40.   else
  41.     result = -1;
  42.   if (result) {        // set directory to current directory, tough luck sonny
  43.     VRefNum = 0;
  44.     DirID = 0;
  45.   }
  46.   result = HOpen(VRefNum, DirID, preffilename, fsCurPerm, &fref);
  47.   if (result)    // no existing file forget it
  48.       return false;
  49.   result = FSRead(fref, &length, p);      // get info
  50.   result = FSClose(fref);
  51.   fref = 0;        // so we don't wipe out any disks (see THINK ref)
  52.   return length;
  53. }
  54.  
  55.  
  56. void DoSavePrefs(Ptr p, long length)
  57. {
  58.   short VRefNum;
  59.   long DirID, ThePrefs;
  60.   short fref;
  61.   OSErr result;
  62.  
  63.   if (IsFindFolder())
  64.     result = FindFolder(kOnSystemDisk, kPreferencesFolderType,
  65.             kDontCreateFolder, &VRefNum, &DirID);
  66.   else
  67.     result = -1;
  68.   if (result) {        // set directory to current directory, tough luck sonny
  69.     VRefNum = 0;
  70.     DirID = 0;
  71.   }
  72.   result = HOpen(VRefNum, DirID, preffilename, fsCurPerm, &fref);
  73.   if (result) {        // no existing file, make a new one
  74.       result = DoCreatePrefsFile(VRefNum, DirID);
  75.       if (result)
  76.           return;        // canna do it
  77.      result = HOpen(VRefNum, DirID, preffilename, fsCurPerm, &fref);
  78.      if (result)
  79.          return;
  80.   }
  81.   result = FSWrite(fref, &length, p);      // get info
  82.   if (!result)
  83.       result = SetEOF(fref, length);        // just in case
  84.   result = FSClose(fref);
  85.   fref = 0;        // so we don't wipe out any disks (see THINK ref)
  86. }